package com.dong.auth.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
 *
 * 控制session
 *
 * @author zhaodongchao
 * @date 2021/10/27 0:46
 * @version V1.0
 */
public class SessionFilter implements Filter {
    static final String FILTER_APPLIED = "__spring_security_session_mgmt_filter_applied";

    private String springSecurityContextKey = "SPRING_SECURITY_CONTEXT" ;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        if (httpServletRequest.getAttribute(FILTER_APPLIED) !=null){
            chain.doFilter(request,response);
            return;
        }
        httpServletRequest.setAttribute(FILTER_APPLIED,Boolean.TRUE);

        HttpSession session = httpServletRequest.getSession(false);
        if (session != null){
            if (session.getAttribute(this.springSecurityContextKey) != null) {

            }
        }
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
